package org.plr.ecommerce.view;

import java.io.Serializable;
import org.apache.wicket.model.Model;
import org.hibernate.SessionFactory;
import org.plr.ecommerce.model.Entity;

public class EntityModel<ENTITY extends Entity> extends Model {

    public EntityModel(Class<ENTITY> entityClass, Serializable entityId) {
        this((ENTITY) Application.get().getBean(SessionFactory.class).getCurrentSession().load(entityClass, entityId));
    }

    public EntityModel(ENTITY entity) {
        super(entity);
    }

    @Override
    public ENTITY getObject() {
        return (ENTITY) super.getObject();
    }

    public void create() {
        Application.get().getBean(SessionFactory.class).getCurrentSession().persist(this.getObject());
    }

    public void read() {
        Application.get().getBean(SessionFactory.class).getCurrentSession().refresh(this.getObject());
    }

    public void update() {
        Application.get().getBean(SessionFactory.class).getCurrentSession().merge(this.getObject());
    }

    public void delete() {
        Application.get().getBean(SessionFactory.class).getCurrentSession().delete(this.getObject());
    }
}
